package com.gitviewer.main;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeListener;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class GitViewerFrame extends javax.swing.JFrame 
	implements WindowListener
{
	/**
	 * 
	 */
	JMenuBar menuBar;
	JMenu fileMenu,viewMenu,helpMenu;
	
	Action exitAction;
	
	Locale locale;
	ResourceBundle bundle;
	
	private static final long serialVersionUID = 6497038656153404159L;

	public GitViewerFrame(Locale locale) {
		super("Git viewer");
		
		this.locale = locale;
		bundle  = ResourceBundle.getBundle(
                "com.gitviewer.main.GitViewerFrame", locale);
		addWindowListener(this);
		
		menuBar = new JMenuBar();
		fileMenu = new JMenu(bundle.getString("FileMenu"));
		try {
			String mnemonic = bundle.getString("FileMenu.mnemonic");
			fileMenu.setMnemonic(mnemonic.charAt(0));
		} catch (MissingResourceException e) {
		}

		exitAction = new ExitAction();
		fileMenu.add(exitAction);
			
		menuBar.add(fileMenu);
		
		setJMenuBar(menuBar);
	}

	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent arg0) {
		System.exit(0);
	}

	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	abstract class I18NAction extends AbstractAction {
		public I18NAction(ResourceBundle bundle, String key) {
			super(bundle.getString(key));
			try {
				String mnemonicString = bundle.getString(key + ".mnemonic");
				KeyStroke mnemonicKeyStroke = KeyStroke.getKeyStroke(mnemonicString);
				int keyCode = mnemonicKeyStroke.getKeyCode();
				putValue(MNEMONIC_KEY, new Integer(keyCode));
			} catch (Exception e) {
			} 
			try {
				String acceleratorString = bundle.getString(key + ".accelerator");
				KeyStroke acceleratorKeyStroke = KeyStroke.getKeyStroke(acceleratorString);
				putValue(ACCELERATOR_KEY, acceleratorKeyStroke);
			} catch (Exception e) {
			} 
		}
	}
	
	class ExitAction extends I18NAction {

		private static final long serialVersionUID = 7563387141746346615L;
		
		public ExitAction() {
			super(bundle, "ExitAction");
		}

		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}
	}

}
